using System;
using System.Data;
using SharePoint.ImportExport.Core;
using GotDotNet.SharePointExtractor;


namespace cmdSharePointExtractor
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class cmdUI
	{
		static string version="RC 2";

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			Parameters parameters=new Parameters();
			if(args.Length<6)
			{
				PrintUsage();
				return;
			}

			if(!parameters.ReadParameters(args))
			{
				PrintUsage();
				return;
			}
			Extractor extractor=new Extractor();
			DataSet ds= new DataSet("Export Parameters");
			DataTable dt=ds.Tables.Add("Parameters");
			
			dt.Columns.Add("SiteURL");
			dt.Columns.Add("OutputDirectory");
			dt.Columns.Add("SiteName");
			dt.Columns.Add("List");
			dt.Columns.Add("Owner");
			dt.Columns.Add("Contact");
			dt.Columns.Add("IncludeSubWebs");
			dt.Columns.Add("RootContainerName");
			dt.Columns.Add("SiteType");
			dt.Columns.Add("DestSiteType");
			dt.Columns.Add("Folder");
			dt.Columns.Add("DateTimeThreshhold");
			dt.Columns.Add("ExtractDesign");
			dt.Columns.Add("ExtractUsers");
			dt.Columns.Add("ShowInSiteDir");
			dt.Columns.Add("DateThreshold");
			dt.Columns.Add("ExtractVersions");
			dt.Columns.Add("ExtractIDs");
			
			DataRow newRow = dt.NewRow();

			// Set values in the columns:
			newRow["SiteURL"] = parameters.SiteURL;
			newRow["OutputDirectory"] = parameters.OutputDirectory;
			newRow["SiteName"] = parameters.NewName;
			newRow["List"] = parameters.List;
			newRow["Owner"] = parameters.Owner;
			newRow["Contact"] = parameters.Contact;
			newRow["includeSubWebs"] = parameters.IncludeSubWebs;
			newRow["RootContainerName"] = parameters.Root;
			newRow["SiteType"] = parameters.SiteType;
			newRow["DestSiteType"] = parameters.DestSiteType;
			newRow["Folder"] = parameters.Folder;
			newRow["DateTimeThreshhold"] = parameters.DateThreshhold;
			newRow["ExtractDesign"] = parameters.ExtractDesign;
			newRow["ExtractUsers"] = parameters.ExtractUsers;
			newRow["ShowInSiteDir"] = parameters.ShowInSiteDir;
			newRow["DateThreshold"] = parameters.Threshold.ToString();
			newRow["ExtractVersions"] = parameters.ExtractVersions;
			newRow["ExtractIDs"] = parameters.ExtractIDs;
			
			// Add the row to the rows collection.
			dt.Rows.Add(newRow);
			Logger.Init("export" + DateTime.Now.ToString("MMddHHmmss") + ".log",ErrorLevel.Information);
			Logger.LogDataSendingEvent += new Logger.LogDataEventHandler(IncomingEventLogData);

			try
			{

				extractor.Process(ds);

			}
			catch(Exception Ex)
			{
				Console.WriteLine("Exception occurred :"+Ex.Message);
			}
			Logger.LogDataSendingEvent -= new Logger.LogDataEventHandler(IncomingEventLogData);
			Logger.Close();

		}
		private static void IncomingEventLogData(object sender, SPEventArgs e)
		{
			string logArgs = DateTime.Now.ToLongTimeString()+ ":  " + e.Message;
			Console.WriteLine(logArgs);
		}


		static void PrintUsage()
		{
			Console.WriteLine("SharePoint data extractor Version {0}", version);
			Console.WriteLine("");
			Console.WriteLine("---");
			Console.WriteLine("Usage:");
			Console.WriteLine("-url URL to the site/area to be extracted");
			Console.WriteLine("-type Source [Site/Area]");
			Console.WriteLine("-newname site/area name of the new site");	
			Console.WriteLine("-desttype Destination type [Site/Area]");
			Console.WriteLine("-out x:\\outputfolder");
			Console.WriteLine("");
			Console.WriteLine("Optional:");
			Console.WriteLine("-root [Sites|Topics]");
			Console.WriteLine("-list Name of list/library to be extracted");
			Console.WriteLine("-folder Name of folder to be extracted");
			Console.WriteLine("-includesubwebs true/false");
			Console.WriteLine("-extractusers true/false");
			Console.WriteLine("-extractdesign true/false");
			Console.WriteLine("-extractversions true/false");
			Console.WriteLine("-extractids Controls if the List item IDs are exported true/false");
			Console.WriteLine("-showinsitedirectory true/false");
			Console.WriteLine("-owner DOMAIN\\User");
			Console.WriteLine("-contact user@domain.com");
			Console.WriteLine("-threshold DateTime threshold, if specified then items newer than the threshold will be migrated");
			//			Console.WriteLine("-reportLevel Information|Warning|Error");
			Console.WriteLine("");
			Console.WriteLine("Press any key to continue...");
			Console.ReadLine();

		}
	}
}

